package com.itestor.iptv.utils;



import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

/**
 * 日期格式工具类.
 */
public class IOUtil {
    /**
     * logger.
     */
    private static final Logger logger = Logger.getLogger(IOUtil.class);

    /**
     * 日期类型转成String.
     * @param date date
     * @param pattern pattern
     * @return String
     */
    public static Map<String,String> getDataFromFile(String fileName) {
//        Class c = IOUtil.class.getClass();
//        URL url = c.getResource(fileName);
//        System.out.println(url);
        File file = new File(fileName);
//        File file = new File((IOUtil.class.getClass().getResource(fileName)).getFile());
        List lines = new ArrayList();
        Map<String,String> map = new LinkedHashMap<String, String>();
        String temp="";
        if(file.exists()){
            try {
                lines = FileUtils.readLines(file, "UTF-8");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(lines.size());
            for(int i=0;i<lines.size();i++){
//                System.out.println(i+"[0]:"+lines.get(i).toString().split("=")[0].trim()+"|[1]:"+lines.get(i).toString().split("=")[1].trim());
                if(lines.get(i)==null||"".equals(lines.get(i).toString().trim())||lines.get(i).toString().trim().indexOf("=")==-1)
                    continue;
                else{
                    System.out.println("........"+lines.get(i).toString()+".........");
                    map.put(lines.get(i).toString().split("=")[0].trim(), lines.get(i).toString().split("=")[1].trim());
                }
            }
        }
        return map;
    }
    
    public static void setDataToFile(Map<String,String> map,String fileName) {
        File file = new File(fileName);
        String content = map.toString().substring(1,map.toString().length()-1);
        String[] contents = content.split(", ");
        content = "";
        for(int i= 0;i<contents.length;i++)
            content = content+contents[i]+System.getProperty("line.separator");
        try {
            FileUtils.writeStringToFile(file, content);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    

    public static void main(String[] args){
        String fileName = "D:/itext/iTestCaptureConf.ini";
        Map<String,String> map = getDataFromFile(fileName);
        System.out.println(map);
        map.put("robot_ip", "192.168.1.");
        System.out.println(map);
//        setDataToFile(map, fileName);
    }
}
